#include "ADUSplash.h"

Scene* SplashLayer::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = SplashLayer::create();
    // add layer as a child to scene
    scene->addChild(layer,1);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool SplashLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
	mTimer = SPLASH_TIMER;
    mBeginIntro = false;
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    float centerX = origin.x + visibleSize.width/2;
    float centerY = origin.y + visibleSize.height/2;

	log("DUNGNT: SPLASH INIT");
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//     log("PLAY VIDEO");
//     // Create video player for Android
//     VideoPlayer* videoPlayer = VideoPlayer::create();
//     videoPlayer->setFullScreenEnabled(true);
//     videoPlayer->setFileName(MOV_FILE);
//     videoPlayer->play();
// #else
    mBackground = Sprite::create(IMG_SPLASH);
    mBackground->setPosition(centerX,centerY);
    this->addChild(mBackground, Z_BACKGROUND);
	this->scheduleUpdate();
// #endif
	return true;
		
}

void SplashLayer::update(float dt) {
    if(mBeginIntro) return;
	mTimer -= dt;
	if(mTimer <= 0) {
        mBeginIntro = true;
		// createIntro();
        nextScene();
	}
}

void SplashLayer::createIntro() {
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vector<SpriteFrame*> animFrames(NUM_IMG_INTRO);
    char str[100] = {0};
    for(int i = 1; i <= NUM_IMG_INTRO; i++)
    {
        sprintf(str, "Intro/Intro%04d.jpg",i);
        auto frame = SpriteFrame::create(str,Rect(0,0,visibleSize.width,visibleSize.height)); //we assume that the sprites' dimentions are 40*40 rectangles.
        animFrames.pushBack(frame);
    }

    auto animation = Animation::createWithSpriteFrames(animFrames, 0.01f);
    auto animate = Animate::create(animation);
    auto actionFinished = CallFunc::create(this, callfunc_selector(SplashLayer::nextScene));
    auto sequence = Sequence::create(animate, actionFinished, NULL);
    mBackground->runAction(sequence);
}

void SplashLayer::nextScene() {
    GameManager::getInstance()->RunScene(INTRO_SCENE);
}